package com.sz.jinzheng.service.impl;

import com.sz.jinzheng.mapper.UserMapper;
import com.sz.jinzheng.model.Role;
import com.sz.jinzheng.service.UserService;
import com.sz.jinzheng.until.UserUntil;
import com.sz.jinzheng.model.BorrowerInfo;
import com.sz.jinzheng.model.User;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserUntil userUntil;

    /**
     * 登录
     */
    @Override
    public User login(User user) {
        return userMapper.login(user);
    }

    /**
     * 检查手机是否已经注册
     *
     * @param //前端传来的参数
     * @return //
     */
    @Override
    public boolean check(String phone) {
        User user1 = userMapper.check(phone);
        boolean f = true;//默认没有注册
        if (user1 != null) {
            //已经注册
            f = false;
        }
        return f;
    }

    /**
     * //新增用户（注册）
     *
     * @param user //用户传过来的user
     */
    @Override
    public int addUser(User user) {
        //1.获取盐
        String salt = userUntil.getSalt();
        user.setSalt(salt);
        //2.md5+盐一起加密
        String pwd = userUntil.getPwd(user.getPassword(), salt);
        //3.将加密的密码放到数据库
        user.setPassword(pwd);
        user.setIs_deleted(1);
        return userMapper.addUser(user);
    }

    @Override
    public User getUserByPhone(String phone) {
        return userMapper.getUserByPhone(phone);
    }


    /**
     * 通过uid查新借款人详情
     *
     * @param uid 用户id
     * @return BorrowerInfo借款人详情表
     */
    @Override
    public BorrowerInfo getBorrowerInfoById(Integer uid) {
        return userMapper.getBorrowerInfoById(uid);
    }

    ;

    /**
     * 通过uid修改用户登录信息
     *
     * @param user
     * @return
     */
    @Override
    public Integer updateUser(User user) {
        return userMapper.updateUser(user);
    }

    /**
     * 通过token修改用户的贷款信息
     *
     * @param borrowerInfo //前端传的贷款信息表
     * @return
     */
    public int updateBorrowerInfoById(BorrowerInfo borrowerInfo) {

        return userMapper.updateBorrowerInfoById(borrowerInfo);
    }


    /**
     * 查询用户权限
     *
     * @param phone 用户手机号
     * @return 返回用户的权限集合
     */
    public List<Role> getRoles(String phone) {
        return userMapper.getRoles(phone);
    }
}
